package com.itcoon.cloud.framework.springdoc.plugin;/**
 * @author Athric Yu
 * 2024/11/21
 **/

import com.itcoon.cloud.framework.constants.CommonCoreConstants;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.ParameterCustomizer;
import org.springframework.core.MethodParameter;

/**
 *
 *
 * @author Athric Yu
 * 2024/11/21
 **/
public class ArrayParameterCustomizer implements ParameterCustomizer {

    @Override
    public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) {
        Class<?> type = methodParameter.getParameterType();
        if(type.isArray()){
            Schema<Object> schema = new ArraySchema();
            Class<?> componentType = type.getComponentType();
            Schema contentSchema = new Schema();
            if(CommonCoreConstants.dateTimeClasses.contains(componentType)){
                contentSchema.setType("dateTime");
            }else if(CommonCoreConstants.dateClasses.contains(componentType)){
                contentSchema.setType("date");
            }else{
                contentSchema.setType(componentType.getSimpleName().toLowerCase());
            }
            schema.setContentSchema(contentSchema);
            parameterModel.setSchema(schema);
        }
        return parameterModel;
    }
}
